/**
 * 标签服务
 */
const { validationResult } = require("express-validator");
const { CODE_Conflict } = require("@root/config/index");
const { poolPromise } = require("@root/db/index");
const { validateParams } = require("@root/utils/peak-tools.js");

/**
 * 获取标签
 * @param {*} req
 * @param {*} res
 * @param {*} next
 */
async function selectTags(req, res, next) {
  if (validateParams({ req, res, validationResult })) return;
  try {
    const select_tags_sql = `select t.*, count(a_t.tag_id) as article_count from tags t left join article_tags a_t on t.tag_id = a_t.tag_id group by t.tag_id`;
    let [list] = await poolPromise.query(select_tags_sql);
    res.status(200).json({
      code: 200,
      msg: "获取标签成功",
      data: list,
    });
  } catch (error) {
    next(error);
  }
}

/**
 * 添加标签
 * @param {*} req
 * @param {*} res
 * @param {*} next
 */
async function insertTag(req, res, next) {
  if (validateParams({ req, res, validationResult })) return;
  try {
    const { tag_name } = req.body;
    const select_tags_sql = `select tag_name from tags where tag_name=?`;
    let [list] = await poolPromise.query(select_tags_sql, [tag_name]);
    if (list.length) {
      return res.status(CODE_Conflict).json({
        code: CODE_Conflict,
        msg: "标签名已存在",
        data: null,
      });
    }
    const insert_tags_sql = `insert into tags(tag_name) values(?)`;
    let [{ affectedRows: tag_affectedRows }] = await poolPromise.execute(insert_tags_sql, [
      tag_name,
    ]);
    if (!tag_affectedRows) {
      return res.status(500).json({
        code: 500,
        msg: "添加标签失败",
        data: null,
      });
    }
    res.status(200).json({
      code: 200,
      msg: "添加标签成功",
    });
  } catch (error) {
    next(error);
  }
}

/**
 * 删除标签
 * @param {*} req
 * @param {*} res
 * @param {*} next
 */
async function deleteTag(req, res, next) {
  if (validateParams({ req, res, validationResult })) return;
  try {
    const { ids } = req.body;
    const delete_tags_sql = `delete from tags where tag_id in (${new Array(ids.length)
      .fill("?")
      .join(",")})`;
    let [{ affectedRows: tag_affectedRows }] = await poolPromise.execute(delete_tags_sql, ids);
    if (!tag_affectedRows) {
      return res.status(500).json({
        code: 500,
        msg: "删除标签失败",
        data: null,
      });
    }

    res.status(200).json({
      code: 200,
      msg: "删除标签成功",
      data: null,
    });
  } catch (error) {
    next(error);
  }
}

/**
 * 修改标签
 * @param {*} req
 * @param {*} res
 * @param {*} next
 */
async function updateTag(req, res, next) {
  if (validateParams({ req, res, validationResult })) return;
  try {
    const { tag_id, tag_name } = req.body;
    const update_tags_sql = `update tags set tag_name = ? where tag_id = ?`;
    let [{ affectedRows: tag_affectedRows }] = await poolPromise.execute(update_tags_sql, [
      tag_name,
      tag_id,
    ]);
    if (!tag_affectedRows) {
      return res.status(500).json({
        code: 500,
        msg: "修改标签失败",
        data: null,
      });
    }
    res.status(200).json({
      code: 200,
      msg: "修改标签成功",
      data: null,
    });
  } catch (error) {
    next(error);
  }
}

module.exports = {
  selectTags,
  insertTag,
  deleteTag,
  updateTag,
};
